package com.briup.server.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.briup.common.response.Result;
import com.briup.server.domain.bean.LabsRentalTime;
import com.briup.server.domain.dto.LabsRentalTimePageQueryDTO;
import com.briup.server.domain.vo.RentalTimeAvailableVO;
import com.briup.server.domain.vo.RentalTimeVO;
import com.briup.server.service.LabsRentalTimeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.FutureOrPresent;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

@Validated
@Tag(name = "实验室预约时间段管理模块")
@RestController
@RequestMapping("/rental/time")
@RequiredArgsConstructor
public class RentalTimeController {

    private final LabsRentalTimeService service;

    @Operation(summary = "分页查询各个实验室的预约时间段")
    @GetMapping("/page")
    public Result<Page<RentalTimeVO>> pageQuery(LabsRentalTimePageQueryDTO queryDTO) {
        Page<RentalTimeVO> page = service.pageQuery(queryDTO);
        return Result.success(page).apply("查询成功");
    }

    @Operation(summary = "新增实验室预约时间段")
    @PostMapping
    public Result<?> save(@RequestBody LabsRentalTime labsRentalTime) {
        service.saveOrUpdateLabsRentalTime(labsRentalTime);
        return Result.success();
    }

    @Operation(summary = "更新实验室预约时间段")
    @PutMapping
    public Result<?> update(@RequestBody LabsRentalTime labsRentalTime) {
        service.saveOrUpdateLabsRentalTime(labsRentalTime);
        return Result.success();
    }

    @Operation(summary = "删除实验室预约时间段（批量）")
    @DeleteMapping
    public Result<?> delete(@RequestBody List<Long> ids) {
        service.removeByIds(ids);
        return Result.success();
    }

    @Operation(summary = "根据实验室id以及对应日期查询可用的预约时间段")
    @GetMapping("/available")
    public Result<List<RentalTimeAvailableVO>> getAvailableTimes(
            @RequestParam("labId") @NotNull(message = "实验室id不能为空") Long labId,
            @RequestParam("date") @NotNull(message = "日期不能为空")
            @FutureOrPresent(message = "日期不能在过去") LocalDate date) {

        List<RentalTimeAvailableVO> availableTimes = service.getAvailableTimes(labId, date);
        return Result.success(availableTimes).apply("查询成功");
    }
}
